<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 11:10
 * 预警管理
 */
class Warning extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('warning_model');

    }

    //预警页面
    public function index()
    {
//        echo 123123;
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }
        $status = (isset($_GET['status'])) ? trim($_GET['status']) : 123;


        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',      //那个搜索
                'arg'=> $this->input->get('search'), //搜索内容
                'side' => 'both'   //like通配符%的位置
            ];

            if($status == '123'){
                $where = 0;
            }else{
                $where = [
                    'status =' => $status
                ];
            }

            $data = $this->warning_model->search('*',$where,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('/admin/warning/list',$viewData);
        } else {

            if($status == '123'){
                $where = 0;
            }else{
                $where = [
                    'status =' => $status
                ];
            }

            $data = $this->warning_model->page('warning',$where,$page_num,$start_position); //limit查询的数据

            if($where){
                $viewData = [
                    'list' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => count($data), //这里得到从数据库中的总页数
                    'pageNum'  => $page_num //每页显示多少条

                ];
            }else{
                $viewData = [
                    'list' => $data[0],  //这里得到从数据库中的当页数据
                    'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                    'pageNum'  => $page_num //每页显示多少条

                ];
            }
            $this->load->view('/admin/warning/list',$viewData);
        }

    }

    //判断post还是get参数
    public function edit()
    {
        $data = [
            'status' => 1,
        ];
        $postData = $this->input->post();

        if ($postData){
            if(!$postData){
                redirect('/admin/warning/index');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->warning_model->edit($where,$data);
            }

            redirect('/admin/warning/index');
        } else {
            $where = [
                'id' => $this->input->get('id')
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->warning_model->edit($data,$where);

            if($resultData){
                echo '<script>alert("修改成功")</script>';
                redirect('/admin/warning/index');
            }
        }





    }


}